package com.haitaoit.chengquan.module.home.adapter.forum;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.base.QBaseActivity;
import com.haitaoit.chengquan.bean.ImgObj;
import com.haitaoit.chengquan.utils.AppLog;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.view.Img;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.ArrayList;
import java.util.List;

public class QImageAdapter extends BaseAdapter {
    private List<ImgObj> list=new ArrayList<ImgObj>();
    private Context context;
    private BackCall backCall;

    public QImageAdapter(Context context) {
        this.context = context;
    }

    public List<ImgObj> getDataList() {
        return list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int arg0) {
        return list.get(arg0);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_qimage, null);
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

       final ImgObj imgObj=list.get(position);
        holder.imageView.setImageResource(R.mipmap.pic23_2);
        if(imgObj.getHaveImg()==0){
            holder.img_delete.setVisibility(View.INVISIBLE);
        }else{
            holder.img_delete.setVisibility(View.VISIBLE);
        }
        ImageLoader.getInstance().displayImage(Constant.locImgPathPrefix+imgObj.getLocImgPath(), holder.imageView, ((QBaseActivity) context).getDisplayImageOptions(R.mipmap.pic23_2));

        holder.imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                backCall.deal(R.id.icon, position);
            }
        });

        holder.img_delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {

                ImgObj imgObj1=list.get(getCount()-1);
                if(imgObj1.getHaveImg()!=0){
                    imgObj1.setHaveImg(0);
                    imgObj1.setLocImgPath("");
                }else{
                    list.remove(position);
                }

                notifyDataSetChanged();

            }
        });

//        DisplayMetrics metrics = convertView.getResources().getDisplayMetrics();
//        ViewGroup.LayoutParams lp1 = holder.icon.getLayoutParams();
//        lp1.height = (metrics.widthPixels - 55) / 3;
//        holder.icon.setLayoutParams(lp1);

//        if (list.size() != 0) {
//            if (position < list.size()) {
//                holder.icon.setImageBitmap(list.get(position).getBmp());
//            }
//        }

        return convertView;
    }

    public class ViewHolder {
        @ViewInject(R.id.imageView)
        ImageView imageView;
        @ViewInject(R.id.img_delete)
        ImageView img_delete;
    }

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }



    private class LocImgLoadListener implements ImageLoadingListener {
        private int index;
        private ImageView imageView_delete;

        LocImgLoadListener(int index,ImageView imageView_delete) {
            this.index = index;
            this.imageView_delete = imageView_delete;
        }

        @Override
        public void onLoadingStarted(String s, View view) {

        }

        @Override
        public void onLoadingFailed(String s, View view, FailReason failReason) {
            AppLog.D("Loc_onLoadingFailed_index:" + index);
            list.get(index).setHaveImg(0);
        }

        @Override
        public void onLoadingComplete(String s, View view, Bitmap bitmap) {
            AppLog.D("Loc_onLoadingComplete_index:" + index);
            ImgObj authObj = list.get(index);
            if (!TextUtils.isEmpty(authObj.getLocImgPath())) {
                authObj.setHaveImg(2);
                imageView_delete.setVisibility(View.VISIBLE);
            }

        }

        @Override
        public void onLoadingCancelled(String s, View view) {

        }
    }

    private class NetImgLoadListener implements ImageLoadingListener {
        private int index;
        private ImageView imageView_delete;

        NetImgLoadListener(int index,ImageView imageView_delete) {
            this.index = index;
            this.imageView_delete = imageView_delete;
        }

        @Override
        public void onLoadingStarted(String s, View view) {
            AppLog.D("Net_onLoadingStarted_index_s:" + s);
        }

        @Override
        public void onLoadingFailed(String s, View view, FailReason failReason) {
            AppLog.D("Net_onLoadingFailed_index:" + index);
            ImgObj authObj = list.get(index);

            if (!TextUtils.isEmpty(authObj.getNetImgPath())&&authObj.getStatus()==1) {
                imageView_delete.setVisibility(View.VISIBLE);
            }


        }

        @Override
        public void onLoadingComplete(String s, View view, Bitmap bitmap) {
//            AppLog.D("Net_onLoadingComplete_index:"+index);
//            AppLog.D("Net_onLoadingComplete_index_s:"+s);
            ImgObj authObj = list.get(index);

            if (!TextUtils.isEmpty(authObj.getNetImgPath())&&authObj.getStatus()==1) {
                imageView_delete.setVisibility(View.VISIBLE);
            }

        }

        @Override
        public void onLoadingCancelled(String s, View view) {

        }
    }

}
